/*
  Copyright (c) 2014 Dimitrij Gester

  "Smart RC TX" is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  "Smart RC TX" is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with "Smart RC TX"; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA
*/


#ifndef EEPROMPERSISTOR_H
#define EEPROMPERSISTOR_H

#include <Arduino.h>
#include <EEPROM.h>
#include "MixerPersistor.h"


class EepromPersistor : public MixerPersistor
{
public:
	EepromPersistor()
	: MixerPersistor()
	, pos(0)
	, EEPROM_SIZE(1024) // for atmega328p
	{}
	
	void setStartAddress(uint16_t b)
	{
		this->pos = b;
	}
	
	void putValue(int16_t v)
	{
		uint8_t h, l;
		h = v >> 8;
		l = v & 0xff;
		
		EEPROM.write(this->pos++, l);
		EEPROM.write(this->pos++, h);
	}
	
	bool hasValue()
	{
		return this->pos < EEPROM_SIZE;
	}
	
	int16_t getValue()
	{
		int16_t value = 0;
		value = EEPROM.read(this->pos++);
		value |= EEPROM.read(this->pos++) << 8;
		
		return value;
	}
	
private:
	uint16_t pos;
	uint16_t EEPROM_SIZE;
};
#endif


